package puzzle.nurikabe.view.swing;

import java.awt.Color;
import java.awt.Graphics2D;

import puzzle.common.view.swing.AbstractPuzzleViewPanel;
import puzzle.nurikabe.data.NurikabeBoard;
import puzzle.nurikabe.data.ShimaState;

public class NurikabeViewPanel extends AbstractPuzzleViewPanel<NurikabeBoard> {

	private static final long serialVersionUID = 1L;

	@Override
	protected void paintBoard(Graphics2D g2, NurikabeBoard b) {
//		Font defaultFont = getFont();
//		Font lightFont = defaultFont.deriveFont(Font.PLAIN);

		for (int y = 0; y < b.height; y++) {
			for (int x = 0; x < b.width; x++) {
				int n = b.getBoardState(x, y);
				if (n != 0) {
					if (n < 0) {
						g2.setColor(Color.BLACK);
						g2.fillRect(x * m_cellSize, y * m_cellSize, m_cellSize, m_cellSize);
						if (b.isAssumed(x, y)) {
							g2.setColor(Color.RED);
							drawStringInCell(g2, x, y, "?");
						}
						// Show kabe closure number
//						g2.setColor(b.isAssumed(x, y) ? Color.RED : Color.DARK_GRAY);
//						g2.setFont(lightFont);
//						drawStringInCell(g2, x, y, String.valueOf(-n));
//						g2.setFont(defaultFont);
					} else {
						Color c;
						ShimaState s = b.getShimaState(n);
						if (s != null) {
							c = s.isFixed() ? Color.WHITE : Color.YELLOW;
						} else {
							c = Color.GREEN;
						}
						g2.setColor(c);
						g2.fillRect(x * m_cellSize, y * m_cellSize, m_cellSize, m_cellSize);
						if (b.isAssumed(x, y)) {
							g2.setColor(Color.RED);
							drawStringInCell(g2, x, y, "?");
						}
					}
				}

				// Show assumed closure
//				g2.setStroke(m_thinStroke);
//				g2.setColor(Color.CYAN);
//				if (b.getClosureNumbers() != null
//						&& b.getAssumedClosureNumber() == b.getClosureNumbers()[x + y * b.width]) {
//					g2.drawRect(x * m_cellSize + 1, y * m_cellSize + 1, m_cellSize - 2, m_cellSize - 2);
//				}

				// Show closure number
//				if (b.getClosureNumbers() != null) {
//					drawStringInCell(g2, x, y, "" + b.getClosureNumbers()[x + y * b.width]);
//				}
			}
		}

		g2.setColor(Color.BLACK);
		for (ShimaState s : b.getShimaStates()) {
			drawStringInCell(g2, s.p.x, s.p.y, String.valueOf(s.n));
		}

		drawGrid(g2, b);
	}
}
